component extends="GenericWrapped" output="False" {

	public any function connect(String serverName, Numeric serverPort) output="False" {
		var socket = CreateObject("java", "org.apache.thrift.transport.TSocket");
		
		socket.init(serverName, serverPort);
		var transport = CreateObject("java", "org.apache.thrift.transport.TFramedTransport").init(socket);
		var protocol = createObject("java", "org.apache.thrift.protocol.TBinaryProtocol").init(transport);
		variables._wrapped = CreateObject("java", "org.apache.cassandra.thrift.Cassandra$Client").init(protocol);
		transport.open();

		return this;
	}

	public Boolean function addKeyspace(required String keyspace, Numeric replicationFactor=1) {
		var ksdef = CreateObject("component", "KeyspaceDef").init(keyspace, "org.apache.cassandra.locator.SimpleStrategy");
		var cfdef = CreateObject("component", "CFDef").init(keyspace, "Column1");
		var strategyOptions = CreateObject("component", "StrategyOptions").init();
		var cfDefList = CreateObject("component", "CFDefList").init();
		var ksAdded = False;

		strategyOptions.put("replication_factor", replicationFactor);

		// ksdef.setName(keyspace);
		// ksdef.setStrategy_class("org.apache.cassandra.locator.SimpleStrategy");
		ksdef.setReplicationFactor(replicationFactor);
		ksdef.setStrategyOptions(strategyOptions);

		// cfdeflist.add(cfdef);
		
		ksdef.setCFDefs(cfdeflist);

		if (Not hasKeyspace(keyspace)) {
			variables._wrapped.system_add_keyspace(ksdef._fetch());
			ksAdded = True;
		}

		return ksAdded;
	}

	public String function removeKeyspace(required String keyspace) {
		var schemaId = "";
		if (hasKeyspace(keyspace)) {
			schemaId = variables._wrapped.system_drop_keyspace(keyspace);
		}
		return schemaId;
	}

	/**
	* determines if the connected instance contains the specified keyspace
	*/
	public Boolean function hasKeyspace(required String keyspace) {
		var ksfound = false;
		var kss = variables._wrapped.describe_keyspaces();
		var kssiter = kss.iterator();
		while (kssiter.hasNext()) {
			ks = kssiter.next();
			if (ks.name.equals(keyspace)) {
				ksfound = true;
				break;
			}
		}
		return ksfound;
	}

	/**
	* returns a list of all the keyspaces in the connected instance.
	*/
	public any function getKeyspaces() {
		return variables._wrapped.describe_keyspaces();
	}


	public any function insert(required String key, 
			required String columnFamily, 
			required String column,
			required String consistencyLevel) {
		var columnParent = CreateObject("java", "org.apache.cassandra.thrift.ColumnParent").init(columnFamily);
		// var column = CreateObject("java", "org.apache.cassandra.thrift.Column").init();
	}

}